#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include "time.h"

#define uint32_t  unsigned int

typedef int (*irq_func)(unsigned short int gpio, void *data);

typedef struct GpioBitInfo {
    unsigned int  groupnumber;
    unsigned int  bitnumber;
    unsigned char value;
    unsigned char direction;
    unsigned char irq_status;
    unsigned char irq_enable;
    irq_func  irq_handler;
    unsigned int irq_type;
    void *data;
} gpio_groupbit_info;

#define GPIO_SET_DIR            _IOWR('w', 4, gpio_groupbit_info)
#define GPIO_GET_DIR            _IOWR('r', 5, gpio_groupbit_info)
#define GPIO_READ_BIT           _IOWR('r', 6, gpio_groupbit_info)
#define GPIO_WRITE_BIT          _IOWR('w', 7, gpio_groupbit_info)
#define GPIO_SET_IRQ            _IOWR('w', 8, gpio_groupbit_info)
#define GPIO_ENABLE_IRQ         _IOWR('w', 10, gpio_groupbit_info)
#define GPIO_DISABLE_IRQ        _IOWR('w', 11, gpio_groupbit_info)


#define IRQ_TYPE_EDGE_FALLING   0x00000002

int main(int argc, char **argv)
{
    printf("\n************************************************\n");
    int gpiofd = 0;
    int ret = 0;
    unsigned int mask = 0;
    gpio_groupbit_info ctrl = {0};

    gpiofd=open("/dev/gpio",O_RDWR);
    //set PH24
    ctrl.bitnumber = 24;
    ctrl.groupnumber = 7;
    ctrl.direction = 1;
    ctrl.value = 0;
    ioctl(gpiofd, GPIO_SET_DIR, &ctrl);
    ioctl(gpiofd, GPIO_WRITE_BIT, &ctrl);
    //PI16
    ctrl.bitnumber = 16;
    ctrl.groupnumber = 8;
    ctrl.direction = 6;
    ctrl.irq_type = 0x00;//上升沿
    ioctl(gpiofd, GPIO_SET_DIR, &ctrl);
    ioctl(gpiofd, GPIO_DISABLE_IRQ, &ctrl);
    ioctl(gpiofd, GPIO_SET_IRQ, &ctrl);
    ioctl(gpiofd, GPIO_ENABLE_IRQ, &ctrl);


    getchar();
    ctrl.bitnumber = 24;
    ctrl.groupnumber = 7;
    ctrl.direction = 1;
    ctrl.value = 1;
    ioctl(gpiofd, GPIO_WRITE_BIT, &ctrl);
    getchar();
    ctrl.value = 0;
    ioctl(gpiofd, GPIO_WRITE_BIT, &ctrl);
    getchar();
    printf("----\n");

    close(gpiofd);
    printf("\n************************************************\n\n");
    
    return 0;
}